userType = -1
var main = {

    init: function() {
        this.initTable();
        this.tabBind()
    },
    initTable: function() {
        loadTable();

        $('#btnAdd').click(function() {
            localStorage.userType = "add";
            parent.$t.goToPage(this, "/user/manage");
        });
    },
    tabBind: function() {
        var that = this;

        layui.use(['form', 'table'], function () {
          var table = layui.table,
              form = layui.form;
            //监听查询
            form.on('submit(formDemo)', function(data){
                loadTable();
                return false;
            });
            form.on('select(userType)', function(data){
                userType = data.value
                return false;
            });

            //监听收起
            form.on('submit(moreSearch)', function (data) {
                if($(this).children().hasClass("fa-chevron-down")){
                    //显示更多条件
                    $(this).parents(".layui-form").find(".more-search").show();
                    //修改更多按钮图标
                    $(this).html('<i class="fa fa-chevron-up">&nbsp;</i>收起筛选');
                }else{
                    //显示更多条件
                    $(this).parents(".layui-form").find(".more-search").hide();
                    //修改更多按钮图标
                    $(this).html('<i class="fa fa-chevron-down">&nbsp;</i>展开筛选');
                }
                return false;
            });

            //监听性别操作
            form.on('switch(sexDemo)', function(obj){
                var flag = obj.elem.checked;
                var status = 1;
                if (flag){
                    status = 1;
                }else {
                    status = 2;
                }
                var userId = obj.elem.dataset.id;

                var json = {"status":status,"userId":userId};
                $.ajax({
                    url: "/user/changeStatus",
                    // 是否异步， false 就是同步
                    async: false,
                    data: json,
                    type: 'post',
                    success: function (result) {
                        if (result.code == property.successCode) {
                            successMsg(result.msg)
                        } else{
                            errorMsg(result.msg);
                        }
                    },
                    error: function (result) {
                        errorMsg(result.msg);
                    }
                });

            });
            

            //监听重置
            $("[type='reset']").click(function () {

              $(this).parents(".layui-form").find("input").val("");
              $(this).parents(".layui-form").find("select").val("");
              form.render();
              loadTable();
              return false;
            });


            //排序方式
            form.on('select(orderBy)',function(){
                loadTable();
                return false;
            })

        });


        //回车触发查询
        $("#userName").keypress(function(e){
            if (e.which == 13){
                loadTable();
            }
        });



    }
}



main.init();

function loadTable(){
    layui.use('table', function(){
        var table = layui.table;
        var form = layui.form;
        var util = layui.util;
        var name = $("#userName").val();
        var  orderBy  =  $("#orderBy").val();
        table.render({
            elem: '#test'
            ,url:"/user/getSysUserList?userName="+name+"&orderBy="+orderBy+"&userType="+userType
            ,title: '用户数据表'
                ,request:{
                    pageName: 'currentPage',
                    limitName: 'size'
                }
            ,cols: [[
                {type:'numbers', title:'序号'}
                ,{field:'userName', title:'用户名'}
                ,{field:'email', title:'邮箱地址'}
                ,{field:'phone', title:'联系电话'}
                ,{field:'createTime', title:'添加时间',
                    templet: function (res) {
                        return  formatDate(res.createTime);
                    }
                }
                ,{field:'status', title:'是否启用', templet: '#switchTpl'}
                ,{title:'操作', toolbar: '#barDemo', width:200}
            ]]
            ,page: true
            ,id : "userListTable"
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del') {
                layer.confirm('确定要删除么',{icon:3, title:'删除确认'}, function(index){
                    var objs = {'userId': data.id};
                    var msg = "删除用户成功";
                    $.ajax({
                        url:'/user/deleteUser' ,
                        // 是否异步， false 就是同步
                        async: false,
                        data: objs || {},
                        type: 'post',
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == property.successCode) {
                                successMsg(result.msg)
                                loadTable();
                                layer.close(index);
                            } else{
                                errorMsg(result.msg);
                            }
                        },
                        error: function (result) {
                            errorMsg("系统异常");
                        }
                    });
                });
            } else if (obj.event === 'edit'){
                localStorage.userType = "edit";
                localStorage.userId = data.id;
                parent.$t.goToPage(this, "/user/manage");
            }else if (obj.event === "detail"){
                localStorage.userType = "detail";
                localStorage.userId = data.id;
                parent.$t.goToPage(this, "/user/manage");
            }else if (obj.event === "setAuth"){
                localStorage.userType = "setAuth";
                localStorage.userId = data.id;
                parent.$t.goToPage(this, "/user/manage");
            }

            else if(obj.event === 'resetPwd') {
                layer.confirm('确定要重置密码么',{icon:3, title:'重置密码确认'}, function(index){
                    var objs = {'userId': data.id};
                    $.ajax({
                        url:'/user/resetPwd' ,
                        // 是否异步， false 就是同步
                        async: false,
                        data: objs || {},
                        type: 'post',
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == property.successCode) {
                                successMsg(result.msg)
                                layer.close(index);
                            } else{
                                errorMsg(result.msg);
                            }
                        },
                        error: function (result) {
                            errorMsg("系统异常");
                        }
                    });
                });
            }
        });
    });
}

